/* GeneScapes is an interactive genome browser to visualize
 * and annotate gene models
 * 
 * Copyright (C) <2012>  <Stefan Lorenz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * See <http://code.google.com/p/genescapes/> for contacts.
 */

package Main;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.UIManager;

import Viewer.Framework;
import Viewer.ReferenceChooser;
import Viewer.Viewer;

/**
 * Main class to start the {@link Framework} of GeneScapes which contains the
 * {@link Viewer} or others panels.
 * 
 * @author Stefan Lorenz<br>
 * <br>
 *         15.11.2012
 */
public class Main {

	/**
	 * Main method
	 * 
	 * @param args
	 */
	public static void main(final String[] args) {
		
		Locale.setDefault(Locale.ENGLISH);
		// trying to load the Nimbus look and feel as standard look
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		}
		catch (Exception e) {
			System.err.println("Can not load Nimbus L&F...continues with standard Look&Feel");
		}

		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {

					Framework framework = new Framework();
					framework.setExtendedState(framework.getExtendedState() | JFrame.MAXIMIZED_BOTH);
					framework.setMinimumSize(new Dimension(600, 400));

					// Loading GeneScapes with command line arguments??
					if (args.length > 0) {
						Viewer viewer = new Viewer(new File(args[0]));
						framework.addViewerToDesktop(viewer);
						for (int i = 1; i < args.length; i++) {
							try {
								ReferenceChooser referenceChooser = new ReferenceChooser(new File(args[i]));
								if(referenceChooser.isLoadable())
									viewer.addTracks(new File(args[i]), i + 1,referenceChooser.getReference(),referenceChooser.getRange());
							}
							catch (IOException e) {
								System.err.println("Can not load " + new File(args[i]).getAbsolutePath());
								System.err.println("Continues with next file...");
								e.printStackTrace();
							}
						}
					}
				}
			});
		}
		catch (Exception e) {
			System.err.println("Sorry, can not start GeneScapes...bye");
			e.printStackTrace();
		}
	}
}
